package com.moli.iov.down.command.design.decorator;

import com.moli.iov.down.command.mq.kafka.KafkaProducer;
import com.moli.iov.down.constants.Constant;
import com.moli.iov.down.model.dto.remoting.CommandDto;
import com.moli.iov.protocol.codec.DataWrapper;
import com.moli.iov.util.ByteArrayUtil;
import com.moli.iov.util.SpringContextUtils;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  kafka 发送命令 被装饰的对象
 * </p>
 *
 * @author: zql
 * @Date: 2018/12/28 14:46
 */
@Slf4j
public class KafkaSendCommand  implements AbstractSendCommand {

    @Override
    public void sendCommand(CommandDto commandDto, byte[] allData) {
        try{
            commandDto.setSendTime(new Date());
            DataWrapper dataWrapper =
                new DataWrapper(String.valueOf(commandDto.getCommandNo()), allData, new Date());
            dataWrapper.setCommand(allData[Constant.COMMAND_INDEX]);

            KafkaProducer kafkaProducer = SpringContextUtils.getBean("kafkaProducer");
            kafkaProducer.send(commandDto.getVin(), dataWrapper);

            String commandStr = ByteArrayUtil.toHexString(commandDto.getCommand());
            log.info("[下行报文] 发送成功 VIN:{} 命令：{} 数据：{}", commandDto.getVin(), commandStr,
                ByteArrayUtil.toHexString(dataWrapper.getAllData()));
        }catch(Exception e){
            log.error("下发命令 kafka 发送命令 异常", e);
        }
    }
}
